//
//  WQCFeedViewController.m
//  renxinghuan
//
//  Created by 王晓民 on 2018/4/3.
//  Copyright © 2018年 吴清翠. All rights reserved.
//

#import "WQCFeedViewController.h"
#import "configuration.h"
#import "MXPhotoView.h"
#import "PlaceholderTextView.h"
#define kTextBorderColor     RGBCOLOR(227,224,216)
#undef  RGBCOLOR

#define RGBCOLOR(r,g,b) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1]
@interface WQCFeedViewController ()<UITextViewDelegate,MXPhotoViewUpdateDelegate>
@property (nonatomic, strong) PlaceholderTextView * textView;
@property (nonatomic, strong)UITextField *textField;
@property (nonatomic, strong) UIButton * sendButton;
@property (nonatomic, strong) NSMutableArray * urlarr;


@end

@implementation WQCFeedViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self  setupNav];
    [self setupPhoto];
    self.urlarr = [[NSMutableArray alloc]init];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
#pragma make -------->>上传图片
- (void)setupPhoto{
   
    
    _textView = [[PlaceholderTextView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width , 200)];
    _textView.backgroundColor = [UIColor whiteColor];
    _textView.delegate = self;
    _textView.font = [UIFont systemFontOfSize:14.f];
    _textView.textColor = [UIColor blackColor];
    _textView.textAlignment = NSTextAlignmentLeft;
    _textView.editable = YES;
    _textView.layer.cornerRadius = 4.0f;
//    _textView.layer.borderColor = kTextBorderColor.CGColor;
    _textView.placeholder = @"请输入您遇到的问题或建议...";
    [self.view addSubview:self.textView];
    UIView * backView = [[UIView alloc] init];
    backView.frame = CGRectMake(0, _textView.frame.size.height+_textView.frame.origin.y, ScreenWidth, 100);
    backView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:backView];
    MXPhotoView *photoView = [[MXPhotoView alloc] init];
    photoView.photoViewDele = self;
    photoView.delegate=self;
    photoView.isNeedMovie = YES;
    photoView.showNum = 4;
    [backView addSubview:photoView];
    
    _textField = [[UITextField alloc]initWithFrame:CGRectMake(0, backView.frame.size.height+backView.frame.origin.y+20 , ScreenWidth, 40)];
    _textField.backgroundColor = [UIColor whiteColor];
    _textField.font = [UIFont systemFontOfSize:14.f];
    _textField.placeholder = @"手机号/邮箱（选填），方便我们联系您";
    _textField.keyboardType = UIKeyboardTypeTwitter;
    UILabel * leftView1 = [[UILabel alloc] initWithFrame:CGRectMake(10,0,10,40)];
    leftView1.backgroundColor = [UIColor clearColor];
    _textField.leftView = leftView1;
    _textField.leftViewMode = UITextFieldViewModeAlways;
    _textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
   [self.view addSubview:_textField];
    
    [self.view addSubview:self.sendButton];
}
- (void)sendFeedBack{
    
    if (_textView.text.length==0) {
        [SVProgressHUD showImage:nil status:@"请输入你的意见"];
    }
    else
    {
    [self loadfeedback];
    }
}

//把回车键当做退出键盘的响应键  textView退出键盘的操作
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    
    if ([@"\n" isEqualToString:text] == YES)
    {
        [textView resignFirstResponder];
        
        
        return NO;
    }
    
    return YES;
}

- (UIButton *)sendButton{
    
    if (!_sendButton) {
        _sendButton = [UIButton buttonWithType:UIButtonTypeCustom];
//        _sendButton.layer.cornerRadius = 2.0f;
        _sendButton.frame = CGRectMake(0, 420, ScreenWidth, 40);
        _sendButton.backgroundColor = wycMain;
        [_sendButton setTitle:@"提交" forState:UIControlStateNormal];
        [_sendButton addTarget:self action:@selector(sendFeedBack) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _sendButton;
    
}
- (void)setupNav
{
    self.navigationItem.title = @"意见反馈";
        [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
        [self.navigationController.navigationBar setBarTintColor:wycMain];
    
    self.view.backgroundColor = WYCGlobalBg;
}

-(void)loadfeedback{
    
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
    [SVProgressHUD setForegroundColor:[UIColor whiteColor]]; //字体颜色
    [SVProgressHUD setBackgroundColor:[UIColor blackColor]];   //背景颜色
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    params[@"content"] = _textView.text;
    NSString *string = [self.urlarr componentsJoinedByString:@","];
    
    params[@"pic"] =string ;
    params[@"contact"] = _textField.text;
    NSLog(@"------chua-%@",params);

    [HttpTool POST:faceback params:params success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"------88-%@",responseObject);
        
        if ([[responseObject objectForKey:@"code"]intValue]  == 0) {
            [SVProgressHUD showImage:nil status:@"提交成功"];
            [self.navigationController popViewControllerAnimated:YES ];
        }else{
            [SVProgressHUD showImage:nil status:[responseObject objectForKey:@"message"]];
            
        }
        
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        //    [self showToast:Failed];
    }];
    
}
-(void)upLoadVideoWithData:(NSData *)data
{
    
    [SVProgressHUD showWithStatus:@"上传中..."];
    
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    NSString *encodedImageStr = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
    params[@"base64Img"] = encodedImageStr;
    NSLog(@"%@",params);
    [HttpTool POST:updateImage params:params success:^(NSURLSessionDataTask *task, id responseObject)
     {
         if ([[responseObject objectForKey:@"code"] intValue] == 0)
         {
            NSString *shouurl = [responseObject objectForKey:@"result"];
             
             [self.urlarr addObject:shouurl];
             [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"上传成功"];

             [SVProgressHUD dismiss];


         }else{

             [SVProgressHUD showImage:[UIImage imageNamed:@""] status:msgStr];
             [SVProgressHUD dismiss];

         }

     } fail:^(NSURLSessionDataTask *task, NSError *error)
     {
         [SVProgressHUD showImage:[UIImage imageNamed:@""] status:Failed];
         [SVProgressHUD dismiss];

     }];
    
    
    
}
@end
